package com.sky.controller.admin;

import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分页查询
    @GetMapping("/page")
    public Result<PageResult> page(String name,int page,int pageSize,Integer type){

        log.info("分页查询,参数：{},{},{},{}",page, pageSize, name,type);

        PageResult pageinfo = categoryService.page(name, page, pageSize, type);

        return Result.success(pageinfo);
    }

    //修改分类
    @PutMapping
    public Result updateCate(@RequestBody Category category){

        Integer cont = categoryService.updateCate(category);

        if(cont != null){
            return Result.success("成功");
        }

        return Result.error("失败");
    }

    //启用、禁用分类
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result upstatus(@PathVariable Integer status,Long id){

        categoryService.upstatus(status,id);

        return Result.success();
    }


    //新增分类
    @PostMapping
    public Result save(@RequestBody Category category){


        Integer save = categoryService.save(category);

        if(save != null){
            return Result.success();
        }

        return Result.error("失败");
    }

    //根据id删除分类
    @DeleteMapping
    public Result remove(Long id){

        Integer cont = categoryService.remove(id);
        if(cont != null){
            return Result.success();
        }
        return Result.error("删除失败");
    }

    //根据类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> getByType(Integer type){
        List<Category> byType = categoryService.getByType(type);
        return Result.success(byType);
    }

}
